package com.yjm.test.Spring5.ch5.aop;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

/**
 * Hello world!
 *
 */
public class Aop 
{
	/*
AOP的类型： AOP 有两种截然不同的类型 ： 静态 AOP 和动态 AOP。 在静态 AOP 中，比如 AspectJ编译时
织入机制所提供的静态 AOP ， 横切逻辑将在编译时应用于代码，如果想要修改逻辑， 必须修改代码并重新
编译 。 而通过动态 AOP（比如 SpringAOP），横切逻辑在运行时动态应用，这样一来就可以更改 AOP 配置而无
须重新编译应用程序 。 这些类型的 AOP 是互补的，并且当它们一起使用时， 就可以为应用程序提供可使用的强大组合。


	 */
	public static void testProxyFactoryBean() {
		GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
		ctx.load("classpath:spring/ch5/proxy-factory-bean.xml");
		ctx.refresh();
		
		Documentarist documentaristOne = ctx.getBean("documentaristOne", Documentarist.class);
		Documentarist documentaristTwo = ctx.getBean("documentaristTwo", Documentarist.class);
		
		System.out.println("Documenarist One >>");
		documentaristOne.execute();
		
		System.out.println("\n\nDocumenarst Two >>");
		documentaristTwo.execute();
		
		ctx.close();
	}
	public static void testAopConfig() {
		GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
		ctx.load("classpath:spring/ch5/aop-config.xml");
		ctx.refresh();
		
		Documentarist documentaristOne = ctx.getBean("documentaristOne", Documentarist.class);
		Documentarist documentaristTwo = ctx.getBean("documentaristTwo", Documentarist.class);
		
		System.out.println("Documenarist One >>");
		documentaristOne.execute();
		
		System.out.println("\n\nDocumenarst Two >>");
		documentaristTwo.execute();
		
		ctx.close();
	}
	public static void testAopConfigComplex() {
		GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
		ctx.load("classpath:spring/ch5/aop-config-complex.xml");
		ctx.refresh();
		
		Documentarist documentaristOne = ctx.getBean("documentaristOne", Documentarist.class);
		Documentarist documentaristTwo = ctx.getBean("documentaristTwo", Documentarist.class);
		
		System.out.println("Documenarist One >>");
		documentaristOne.execute();
		
		System.out.println("\n\nDocumenarst Two >>");
		documentaristTwo.execute();
		
		ctx.close();
	}
	public static void testAopConfigMixture() {
		GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
		ctx.load("classpath:spring/ch5/aop-config-annotation.xml");
		ctx.refresh();
		
		Documentarist documentarist = ctx.getBean("documentarist", Documentarist.class);
		System.out.println("Documenarist >>");
		documentarist.execute();

		ctx.close();
	}
	//tb766572975
	
	@Configuration
	@ComponentScan(basePackages="com.yjm.test.Spring5.ch5.aop")
	@EnableAspectJAutoProxy(proxyTargetClass=true)
	public static class AppConfig{
		
	}
	public static void testAopConfigAnnotation() {
		GenericApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);

		Documentarist documentarist = ctx.getBean("documentarist", Documentarist.class);
		System.out.println("Documenarist >>");
		documentarist.execute();

		ctx.close();
	}


    public static void main( String[] args )
    {
    	testProxyFactoryBean();
		System.out.println("\n\n");
		testAopConfig();
		System.out.println("\n\n");
		testAopConfigComplex();
		System.out.println("\n\n");
		testAopConfigMixture();
		System.out.println("\n\n");
		testAopConfigAnnotation();
    }
}
